package com.ptppay.cf.coin.service.btc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.bitcoinj.core.ECKey;

import com.ptppay.cf.core.business.service.CFService;

import cn.finder.bitcoin.Btc;
import cn.finder.ui.webtool.QueryCondition;
import cn.finder.wae.business.domain.Message;
import cn.finder.wae.business.domain.TableQueryResult;
import cn.finder.wae.business.dto.MapParaQueryConditionDto;
import cn.finder.wae.common.comm.AppApplicationContextUtil;
import cn.finder.wae.queryer.handleclass.QueryerDBAfterClass;

/**
 * @author: finder
 * @function: 创建一个 随机空密钥对
 */
public class CreateSPKeyAfterClass extends QueryerDBAfterClass {
    private Logger logger = Logger.getLogger(CreateSPKeyAfterClass.class);

    CFService cFService = AppApplicationContextUtil.getContext().getBean("cFService", CFService.class);

    @Override
    public TableQueryResult handle(TableQueryResult tableQueryResult,
                                   long showTableConfigId, QueryCondition<Object[]> condition) {
        super.handle(tableQueryResult, showTableConfigId, condition);

        logger.info("===" + this.getClass());
        @SuppressWarnings("unchecked")
        Map<String, Object> data = ((MapParaQueryConditionDto<String, Object>) condition).getMapParas();

        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();

        Map<String, Object> returnItem = new HashMap<String, Object>();
        resultList.add(returnItem);
        tableQueryResult.setResultList(resultList);

        tableQueryResult.setPageIndex(condition.getPageIndex());
        tableQueryResult.setPageSize(condition.getPageSize());
        tableQueryResult.setCount(1l);



        if (tableQueryResult.getMessage().getStatusCode() == Message.StatusCode_OK) {

            try {

                 
                  ECKey eckey= new ECKey();
           		  String privateKey=eckey.getPrivKey().toString();
           		  String address=eckey.toAddress(Btc.get().getNetworkParameters()).toBase58();
                 
                  returnItem.put("private_key", privateKey);
                  returnItem.put("wallet_address", address);
                        
            } catch (Exception e) {
                tableQueryResult.getMessage().setResultCode(Message.StatusCode_ServerError);
                tableQueryResult.getMessage().setMsg("create wallet failed.");
            }

        }


        return tableQueryResult;

    }

    @Override
    public void setJDBCDataSource(DataSource dataSource) {
        super.setJdbcDataSource(dataSource);
    }


}
